package com.team.arms.tijian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.tijian.entity.dto.Citydto;
import com.team.arms.tijian.entity.dto.Maindto;
import com.team.arms.tijian.entity.dto.Towndto;
import com.team.arms.tijian.entity.vo.AllVO;
import com.team.arms.tijian.entity.vo.RecommendVo;
import com.team.arms.tijian.mapper.RecommendedMapper;
import com.team.arms.tijian.mapper.SysHealthExaminationMapper;
import com.team.arms.tijian.service.IRecommendedService;
import entity.Recommended;
import entity.SysHealthExamination;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author huazige
 * @Date 2022/6/6 13:01
 * @Version 1.0
 */

@RestController
@RequestMapping("/audit")
@CrossOrigin
@Api(tags="体检-三个审核模块")
public class AuditController {
    @Autowired
    RecommendedMapper recommendedMapper;

    @Autowired
    SysHealthExaminationMapper sysHealthExaminationMapper;

    @Autowired
    IRecommendedService iRecommendedService;

    @ApiOperation("获取镇武装部审核")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @GetMapping(value = "/gettown")
    public AjaxResult getretown() {
        String healthExaminStats="待镇武装部审核";
        List<RecommendVo> list = recommendedMapper.select(healthExaminStats);
//

        if (list != null) {
            return AjaxResult.success("查询成功", list);
        } else {
            return AjaxResult.error("查询失败");
        }
    }
    @ApiOperation(value = "镇武装部审核-下拉框查找")
    @GetMapping("/gettownbyname")
    public AjaxResult getretownbyname(@RequestParam(value = "查询的字段") String field , @RequestParam(value = "查询的内容")String content) {
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据
        String healthExaminStats="待镇武装部审核";
        List<RecommendVo> list = recommendedMapper.selectbyname(field,content,healthExaminStats);

        if (list.size()>0){
            return AjaxResult.success("查找成功",list);
        }else {
            return    AjaxResult.success("没有找到信息");
        }

    }
    @ApiOperation("获取市武装部审核")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @GetMapping(value = "/getcity")
    public AjaxResult getcity() {
        String healthExaminStats="待市武装部审核";
        List<RecommendVo> list = recommendedMapper.select(healthExaminStats);
//
        if (list != null) {
            return AjaxResult.success("查询成功", list);
        } else {
            return AjaxResult.error("查询失败");
        }
    }
    @ApiOperation(value = "市武装部审核-下拉框查找")
    @GetMapping("/getcitybyname")
    public AjaxResult getcitybyname(@RequestParam(value = "查询的字段") String field , @RequestParam(value = "查询的内容")String content) {
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据
        String healthExaminStats="待市武装部审核";
        List<RecommendVo> list = recommendedMapper.selectbyname(field,content,healthExaminStats);

        if (list.size()>0){
            return AjaxResult.success("查找成功",list);
        }else {
            return    AjaxResult.success("没有找到信息");
        }

    }
    @ApiOperation("获取主检审核")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @GetMapping(value = "/getmain")
    public AjaxResult getreconfirm() {
        String healthExaminStats="待主检审核";
        List<RecommendVo> list = recommendedMapper.select(healthExaminStats);
//

        if (list != null) {
            return AjaxResult.success("查询成功", list);
        } else {
            return AjaxResult.error("查询失败");
        }
    }
//    @ApiOperation("下拉框检索体检信息")
//    @GetMapping("/selectByFile")
//    public AjaxResult selectByField(){
//        return AjaxResult.success(bingyiRegistInfoMapper.selectexamineByFiled(field,content));
//    }
    @ApiOperation(value = "主检审核-下拉框查找")
    @GetMapping("/getmainbyname")
    public AjaxResult getreconfirmbyname(@RequestParam(value = "查询的字段") String field , @RequestParam(value = "查询的内容")String content) {
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据
        String healthExaminStats="待主检审核";
        List<RecommendVo> list = recommendedMapper.selectbyname(field,content,healthExaminStats);

        if (list.size()>0){
            return AjaxResult.success("查找成功",list);
        }else {
            return    AjaxResult.success("没有找到信息");
        }

    }


    @ApiOperation(value = "镇武装部审核")
    @PostMapping("/town")
    public AjaxResult audittown(@RequestBody Towndto towndto) {
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据


            QueryWrapper<Recommended> audit = new QueryWrapper<>();
            audit.eq("examine_id", towndto.getExamineId());
//                .select("z_political_info_id")
//                .last("limit 1");

            Recommended recommended = recommendedMapper.selectOne(audit);

            recommended.setTownAuditReason(towndto.getTownAuditReason());
            recommended.setTownAuditResult(towndto.getTownAuditResult());
            int update = recommendedMapper.update(recommended, audit);//前新后旧

            QueryWrapper<entity.SysHealthExamination> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("examine_id", towndto.getExamineId());
            SysHealthExamination sysHealthExamination = sysHealthExaminationMapper.selectOne(QueryWrapper);
        System.out.println("审核结果为"+towndto.getTownAuditResult());
        String townAuditResult = towndto.getTownAuditResult();
        if (townAuditResult.equals("1")) {
            sysHealthExamination.setHealthExaminStats("待市武装部审核");
            int update2 = sysHealthExaminationMapper.update(sysHealthExamination, QueryWrapper);

            if (update > 0 && update2 > 0) {
                return AjaxResult.success("审核成功");
            } else {
                return AjaxResult.error("审核失败");
            }
        }
        else {
            sysHealthExamination.setHealthExaminStats("审核不通过");
            int update2 = sysHealthExaminationMapper.update(sysHealthExamination, QueryWrapper);
            return   update2 > 0 ? AjaxResult.success("该人员审核不通过") : AjaxResult.error("审核失败");

        }

    }
    @ApiOperation(value = "市武装部审核")
    @PostMapping("/city")
    public AjaxResult auditcity(@RequestBody Citydto citydto) {
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据
        QueryWrapper<Recommended> audit = new QueryWrapper<>();
        audit.eq("examine_id",citydto.getExamineId());
//                .select("z_political_info_id")
//                .last("limit 1");

        Recommended recommended = recommendedMapper.selectOne(audit);

        recommended.setTownAuditReason(citydto.getCityAuditReason());
        recommended.setTownAuditResult(citydto.getCityAuditResult());
        int update = recommendedMapper.update(recommended, audit);//前新后旧

        QueryWrapper<entity.SysHealthExamination> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("examine_id",citydto.getExamineId());
        SysHealthExamination sysHealthExamination = sysHealthExaminationMapper.selectOne(QueryWrapper);
        if (citydto.getCityAuditResult().equals("1")) {
            sysHealthExamination.setHealthExaminStats("待主检审核");
            int update2 = sysHealthExaminationMapper.update(sysHealthExamination, QueryWrapper);
            return   update>0&&update2 > 0 ? AjaxResult.success("审核成功") : AjaxResult.error("审核失败");
             }

        else {
            sysHealthExamination.setHealthExaminStats("审核不通过");
            int update2 = sysHealthExaminationMapper.update(sysHealthExamination, QueryWrapper);
            return   update2 > 0 ? AjaxResult.success("该人员审核不通过") : AjaxResult.error("审核失败");
        }

    }
    @ApiOperation(value = "主检审核")
    @PostMapping("/main")
    public AjaxResult auditmain(@RequestBody Maindto maindto) {
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据
        QueryWrapper<Recommended> audit = new QueryWrapper<>();
        audit.eq("examine_id", maindto.getExamineId());
//                .select("z_political_info_id")
//                .last("limit 1");

        Recommended recommended = recommendedMapper.selectOne(audit);

        recommended.setTownAuditReason(maindto.getMainAuditReason());
        recommended.setTownAuditResult(maindto.getMainAuditResult());
        int update = recommendedMapper.update(recommended, audit);//前新后旧

        QueryWrapper<entity.SysHealthExamination> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("examine_id", maindto.getExamineId());
        SysHealthExamination sysHealthExamination = sysHealthExaminationMapper.selectOne(QueryWrapper);

        if (maindto.getMainAuditResult().equals("1")) {
            sysHealthExamination.setHealthExaminStats("审核通过");
            int update2 = sysHealthExaminationMapper.update(sysHealthExamination, QueryWrapper);
            return   update>0&&update2 > 0 ? AjaxResult.success("审核成功") : AjaxResult.error("审核失败");
        }

        else {
            sysHealthExamination.setHealthExaminStats("审核不通过");
            int update2 = sysHealthExaminationMapper.update(sysHealthExamination, QueryWrapper);
            return   update2 > 0 ? AjaxResult.success("该人员审核不通过") : AjaxResult.error("审核失败");
        }

    }
    @ApiOperation("获取某个人的详情内容")
    @GetMapping(value = "/getall/{examineId}}")
    public AjaxResult selectall(
            @PathVariable("examineId")String examineId
    ) {

        List<AllVO> list = sysHealthExaminationMapper.select(examineId);
        return AjaxResult.success("查询成功",list) ;

    }


}
